//
//  LayoutCellList.m
//  ebook
//
//  Created by 吴超飞 on 2017/3/15.
//  Copyright © 2017年 Moton. All rights reserved.
//

#import "LayoutCellList.h"
#import "UIView+Extension.h"

@interface LayoutCellList()<UITableViewDelegate,UITableViewDataSource>{
    BOOL hasMore;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) BOOL isShowCoverImage;//是否显示封面图片

@end

@implementation LayoutCellList

- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    [self addSubview:self.tableView];
}

-(UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, MTScreenW, 200) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return kLayoutCellListItemHeight;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return self.isNoHead?0:TitleHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MTScreenW, TitleHeight)];
    view.backgroundColor = [UIColor whiteColor];
    [view addSubview:[self duplicate:self.titleLabel]];
    [view addSubview:[self duplicate:self.moreBtn]];
    
    return view;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    LayoutCellListItem *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
    if (!cell) {
        cell = [[LayoutCellListItem alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellId isShowCoverImage:YES];
    }
    StoreBookModel *model = [self.dataArray objectAtIndex:indexPath.row];
    cell.model = model;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if([self.delegate respondsToSelector:@selector(layoutCellBase:didSelectModel:)]&&indexPath.row<self.dataArray.count){
        [self.delegate layoutCellBase:self didSelectModel:self.dataArray[indexPath.row]];
    }
}

//复制view
- (UIView*)duplicate:(UIView*)view
{
    NSData * tempArchive = [NSKeyedArchiver archivedDataWithRootObject:view];
    return [NSKeyedUnarchiver unarchiveObjectWithData:tempArchive];
}

-(void)reloadData{
    
    self.tableView.height = [LayoutCellList viewHeight:self.layOutType isNoHead:_isNoHead];
    [self.tableView reloadData];
}

+(float)viewHeight:(LayoutType *)layoutType{

    return [LayoutCellList viewHeight:layoutType isNoHead:NO];
}

+(float)viewHeight:(LayoutType *)layoutType isNoHead:(BOOL)isNohead{
    
    return isNohead?0:TitleHeight + kLayoutCellListItemHeight*layoutType.rows;;
}

@end
